package org.wsjy.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.wsjy.model.TCulture;
import org.wsjy.service.CultureService;

/**
 * 系统初始化监听，用来随系统启动的一些基础数据
 *
 * @Title:
 * @Company:万世极缘工作室
 * @Description:
 * @Author:万世极缘
 * @Since:2016年2月14日
 * @Copyright:Copyright (c) 2016
 * @ModifyDate:
 * @Version:1.1.0
 */
public class SystemInitListener implements ServletContextListener {
    private ServletContext context = null;
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // 销毁所有的信息
        this.context.removeAttribute("culture");
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        this.context = sce.getServletContext();
        // 将公司信息注入成为culture(公司信息也在公司文件表中)
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        CultureService cultureService = (CultureService) wac.getBean("cultureService");
        TCulture culture = cultureService.getNewest();
        context.setAttribute("culture", culture);
    }

    
}
